<?php 
namespace app\App\controller;

use app\App\validate\UserValidate;
use plugin\saiadmin\basic\AppBaseController;
use app\App\validate\DeviceValidate;
use support\Log;
use support\Request;
use support\Response;
use app\App\model\DeviceModel;

// APP设备模块
class DeviceController extends AppBaseController{

    public function __construct()
    {
        $this->bareModel = new DeviceModel();
        $this->validate = new DeviceValidate();
        parent::__construct();
    }


    // 添加设备（配网成功后）
    public function addDevice($request){
        if(!$this->validate->scene('add')->check($request->post())){
             return $this->fail($this->validate->getError());
        }

        $params = $request->post();
        $device = $this->bareModel->where('device_id', $params['device_id'])->find();
        if($device){
            return $this->fail(trans('device.device_exist'));
        }
        Log::info('参数：', $params);
        $data = array_merge($params, [ 'user_id' => $this->userId, 'id'=>null ]);
        $result_flag = $this->bareModel->save($data);
        if($result_flag!==false){
            return $this->success(trans('common.success'));
        }
        return $this->fail(trans('common.fail'));
    }

    // 删除设备
    public function deleteDevice($request){
        if(!$this->validate->scene('delete')->check($request->post())){
             return $this->fail($this->validate->getError());
        }

        $device_id = $request->post('device_id','');
        $device = $this->bareModel->where('device_id', $device_id)->where('user_id', $this->userId)->find();
        if(!$device){
            return $this->fail(trans('device.device_not_exist'));
        }

        $result_flag = $this->bareModel->where('device_id', $device_id)->where('user_id', $this->userId)->delete();
        if($result_flag!==false){
            return $this->success(trans('common.success'));
        }
        return $this->fail(trans('common.fail'));
    }

    // 设备列表
    public function listDevice($request){
        $deviceList = $this->bareModel->where('user_id', $this->userId)->select()->toArray();
        return $this->success($deviceList);
    }

    // 更新设备信息（主要是名称）
    public function updateInfo($request){
        if(!$this->validate->scene('update')->check($request->post())){
             return $this->fail($this->validate->getError());
        }
        $device_id = $request->post('device_id','');
        $device_name = $request->post('device_name','');
        $device = $this->bareModel->where('device_id', $device_id)->where('user_id', $this->userId)->find();
        if(!$device){
            return $this->fail(trans('device.device_not_exist'));
        }

        $result_flag = $this->bareModel->where('device_id', $device_id)->where('user_id', $this->userId)->update([
            'device_id' => $device_id,
            'device_name' => $device_name,
        ]);
        if($result_flag!==false){
            return $this->success(trans('common.success'));
        }
        return $this->fail(trans('common.fail'));
    }
}